﻿<%@ Page Language="C#" %>
<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        string filePath = SiteContext.Current.FileName;
        try
        {
            filePath=  Server.MapPath(filePath);
        }
        catch{}

        System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        if (fileInfo.Exists)
        {
            Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
            Response.AddHeader("Content-Length", fileInfo.Length.ToString());
            Response.AddHeader("Content-Transfer-Encoding", "binary");
            Response.ContentType = "application/octet-stream";                    
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            Response.WriteFile(fileInfo.FullName);
        }
        else
        {                    
            string script = string.Format("<script>alert('文件{0}不存在，无法进行下载。')</{1}script>", SiteContext.Current.FileName,string.Empty);
            Response.Write(script);            
        }
        Response.Flush();
        Response.End();
    }
</script>

